package com.zhledu.pi;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Random;

public class PiMapper extends Mapper<LongWritable, Text,Text,LongWritable> {

    Random random =new Random();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
              Long pointNum=Long.parseLong(value.toString());//999
              for(long i=0;i<pointNum;i++){
                  double x=random.nextDouble()-0.5;
                  double y=random.nextDouble()-0.5;
                  double d=Math.sqrt(x*x+y*y);
                  LongWritable result=new LongWritable(0);
                  if(d<=0.5){
                      result=new LongWritable(1);
                  }
                  context.write(value,result);
              }
    }
}
